package org.crc.myvhrb.controller.system.basic;

import org.crc.myvhrb.model.Menu;
import org.crc.myvhrb.model.RespBean;
import org.crc.myvhrb.model.Role;
import org.crc.myvhrb.service.MenuService;
import org.crc.myvhrb.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * CRC
 * 2021/8/9
 */
@RestController
@RequestMapping("/system/basic/permiss")
public class PermissController {

    @Autowired
    RoleService roleService;

    @Autowired
    MenuService menuService;

    // 获取角色列表
    @GetMapping("/")
    public List<Role> getAllRoles() {
        return roleService.getAllRoles();
    }

    // 获取所有角色权限菜单
    @GetMapping("/menus")
    public List<Menu> getAllMenus() {
        return menuService.getAllMenus();
    }

    // 获取指定角色权限菜单
    @GetMapping("/mids/{rid}")
    public List<Integer> getMidsByRid(@PathVariable Integer rid) {
        return menuService.getMidsByRid(rid);
    }

    // 添加角色
    @PostMapping("/")
    public RespBean addRole(@RequestBody Role role) {
        if (roleService.addRole(role) == 1) {
            return RespBean.success("添加成功");
        }
        return RespBean.error("添加失败");
    }

    // 删除角色
    @DeleteMapping("/{id}")
    public RespBean deleteRole(@PathVariable int id) {
        if (roleService.deleteRole(id) == 1) {
            return RespBean.success("删除成功");
        }
        return RespBean.error("删除失败");
    }

    @PutMapping("/")
    public RespBean upDateRole(Integer rid,Integer[] mids){
        if (menuService.updateMenuRole(rid,mids)){
            return RespBean.success("更新成功");
        }
        return RespBean.error("更新失败");
    }

}
